package com.common.utils;

import org.slf4j.helpers.MessageFormatter;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author xin.ding
 * @date 2021/4/29 17:54
 */
public class CommonException extends RuntimeException {

    /**
     * @param messageFormat 错误信息描述，支持{}方式参数传递
     * @param arguments     参数
     */

    public CommonException(String messageFormat, Object... arguments) {
        super(MessageFormatter.arrayFormat(messageFormat, arguments).getMessage());
    }


    /**
     * 获取错误信息
     *
     * @param obj
     * @return
     */
    private static String getMessage(Object obj) {
        if (obj == null) {
            return "";
        }

        if (obj instanceof Throwable) {
            StringWriter str = new StringWriter();
            PrintWriter pw = new PrintWriter(str);
            ((Throwable) obj).printStackTrace(pw);
            return str.toString();
        } else {
            return obj.toString();
        }
    }
}
